﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace WPTrek.Gui.Controls
{
    /// <summary>
    /// Interaction logic for HoneycomGrid.xaml
    /// </summary>
    public partial class HoneycomGrid : UserControl
    {
        #region Dependency properties

        #region Columns

        public static readonly DependencyProperty ColumnsProperty =
            DependencyProperty.Register("Columns", typeof(int), typeof(HoneycomGrid),
            new PropertyMetadata(5, OnColumnsPropertyChanged));

        public int Columns
        {
            get
            {
                return (int)GetValue(ColumnsProperty);
            }
            set
            {
                SetValue(ColumnsProperty, value);
            }
        }

        private static void OnColumnsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HoneycomGrid grid = d as HoneycomGrid;
            if (grid != null)
                grid.DrawCells();

        }

        #endregion

        #region Rows

        public static readonly DependencyProperty RowsProperty =
            DependencyProperty.Register("Rows", typeof(int), typeof(HoneycomGrid),
            new PropertyMetadata(5, OnRowsPropertyChanged));

        public int Rows
        {
            get
            {
                return (int)GetValue(RowsProperty);
            }
            set
            {
                SetValue(RowsProperty, value);
            }
        }

        private static void OnRowsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HoneycomGrid grid = d as HoneycomGrid;
            if (grid != null)
                grid.DrawCells();
        }

        #endregion

        #endregion

        public HoneycomGrid()
        {
            InitializeComponent();

            SizeChanged += new SizeChangedEventHandler(HoneycomGrid_SizeChanged);
        }

        void HoneycomGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DrawCells();
        }

        #region Private fields

        private void DrawCells()
        {
            LayoutRoot.Children.Clear();

            double width = LayoutRoot.ActualWidth;
            double cellWidth = 4 * width / (3 * Columns + 1);

            double layoutHeight = LayoutRoot.ActualHeight;

            int wholeRows = (int)(layoutHeight / cellWidth);
            int mixedRows = 2 * wholeRows - 1;

            double lastRowDiff = layoutHeight % cellWidth;
            if (lastRowDiff > cellWidth / 2)
                ++mixedRows;

            int drawRaws = Math.Min(mixedRows, Rows);

            for (int i = 0; i < drawRaws; ++i)
            {
                for (int j = 0; j < Columns / 2; ++j)
                {
                    HoneycombCell cell = new HoneycombCell(i, j)
                    {
                        Width = cellWidth,
                        Height = cellWidth
                    };

                    LayoutRoot.Children.Add(cell);

                    if (i % 2 == 0)
                    {
                        Canvas.SetTop(cell, i * cellWidth / 2);
                        Canvas.SetLeft(cell, 3 * j * cellWidth / 2);
                    }
                    else
                    {
                        Canvas.SetTop(cell, i * cellWidth / 2);
                        Canvas.SetLeft(cell, 3 * (2 * j + 1) * cellWidth / 4);
                    }

                }
            }
        }

        #endregion
    }
}
